/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.upnp;

import java.util.ArrayList;
import java.util.List;

import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.upnp.UPnPDevice;
import org.osgi.util.tracker.ServiceTracker;

public class UpnpList {
	
//	private static final String CONTENT_DIRECTORY = "ContentDirectory";
//	private static final String BROWSE = "Browse";
//	private static final String LIST = "list";
//	private static final String BROWSER = "browser";
//	private static final String ICON = "icon";
	
	private final ServiceTracker upnpDeviceTracker;
	
	public UpnpList(ServiceTracker tracker) {
		this.upnpDeviceTracker = tracker;
	}
	
	public void getList() throws Exception {

		ServiceReference[] serviceReferences = this.upnpDeviceTracker.getServiceReferences();
		System.out.println("serviceReferences Count=" + this.upnpDeviceTracker.getTrackingCount());
		if (serviceReferences == null) {
			System.out.println("#### Unget upnpDeviceTracker ServiceReference ####");
			return;
		}
		
		List<UpnpBean> list = new ArrayList<UpnpBean>();
		for (ServiceReference serviceReference : serviceReferences) {
			Object modelName = serviceReference.getProperty(UPnPDevice.MODEL_NAME);
			Object friendlyName = serviceReference.getProperty(UPnPDevice.FRIENDLY_NAME);
			Object serviceId = serviceReference.getProperty(Constants.SERVICE_ID);
			
			UpnpBean upnpBean = new UpnpBean();
			upnpBean.modelName = (String)modelName;
			upnpBean.friendlyName = (String)friendlyName;
			System.out.println("upnpBean.modelName=" + upnpBean.modelName);
			System.out.println("upnpBean.friendlyName=" + upnpBean.friendlyName);
			upnpBean.serviceId = Long.toString((Long)serviceId);
			list.add(upnpBean);

			// labelに戻す
			UpnpViewComponent component = new UpnpViewComponent();
			component.labelMediaName.setText(upnpBean.modelName);
			component.labelUpnpList.setText(upnpBean.friendlyName);
		}
		
		
		
		
	}
    
//    private void icon(HttpServletRequest req, HttpServletResponse res) throws IOException {
//        final long serviceId;
//        try {
//        	serviceId = Long.parseLong(req.getParameter("serviceId"));
//        } catch (RuntimeException e) {
//            e.printStackTrace();
//            printDefaultIcon(res);
//            return;
//        }
//
//        final ServiceReference[] refs = this.upnpDeviceTracker.getServiceReferences();
//        if (refs == null) {
//            printDefaultIcon(res);
//            return;
//        }
//
//        final UPnPDevice device = getUPnPDevice(serviceId);
//        if (device == null) {
//            printDefaultIcon(res);
//            return;
//        }
//
//        final UPnPIcon[] icons = device.getIcons(null);
//        if (icons == null) {
//            printDefaultIcon(res);
//            return;
//        }
//
//        for (UPnPIcon icon : icons) {
//
//            final InputStream is = icon.getInputStream();
//            final OutputStream os = res.getOutputStream();
//            while (true) {
//                int i = is.read();
//                if (i < 0) {
//                    return;
//                }
//                os.write(i);
//            }
//        }
//    }
    
//    private UPnPDevice getUPnPDevice(long id) {
//        final ServiceReference[] refs = this.upnpDeviceTracker.getServiceReferences();
//        if (refs == null) {
//            return null;
//        }
//        for (ServiceReference ref : refs) {
//            final Long serviceId = (Long) ref.getProperty(Constants.SERVICE_ID);
//            if (id == serviceId.longValue()) {
//                final UPnPDevice device = (UPnPDevice) this.bundleContext.getService(ref);
//                return device;
//            }
//        }
//        return null;
//    }
    
//    private void printDefaultIcon(HttpServletResponse res) throws IOException {
//        final InputStream is = this.getClass().getResourceAsStream("default.png");
//        res.addHeader("Content-Type", "image/png");
//        final OutputStream os = res.getOutputStream();
//        while (true) {
//            int i = is.read();
//            if (i < 0) {
//                return;
//            }
//            os.write(i);
//        }
//    }
    
}
